package view
{
	import com.greensock.events.TransformEvent;
	import com.greensock.transform.TransformItem;
	import com.greensock.transform.TransformManager;
	
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class CutArea extends Sprite
	{
		[Embed(source="icon/uploadBg.png")]
		private var uploadBg:Class;
		
		private var _bg:Bitmap;
		private var _width:Number;
		private var _height:Number;
		
		private var _loader:Loader;
		private var _cutMask:CutMask;
		private var _tm:TransformManager;
		public function CutArea()
		{
			super();
			_bg=new uploadBg;
			_width=_bg.width;
			_height=_bg.height;
			addChild(_bg);
			var g:Graphics=graphics;
			g.lineStyle(1,0xcccccc)
			g.drawRect(0,0,_width,_height);
			g.endFill();
			_cutMask=new CutMask;
		}
		public function load(by:ByteArray):void
		{
			if(_loader){
				if(_loader.parent){
					_loader.parent.removeChild(_loader);
				}
				_loader.contentLoaderInfo.removeEventListener(Event.INIT,onInit);
				_loader.unloadAndStop();
			}
			_loader=new Loader;
			_loader.contentLoaderInfo.addEventListener(Event.INIT,onInit);
			_loader.loadBytes(by);
		}
		private var _contentWidth:Number;
		private var _contentHeight:Number;
		private var scale:Number
		private function onInit(e:Event):void
		{
			_bg.visible=false;
			_contentWidth=_loader.width;
			_contentHeight=_loader.height;
			if(_contentWidth>width&&_contentHeight>height)
			{
				scale=_contentWidth/width>_contentHeight/height?width/_contentWidth:height/_contentHeight;
			}
			else if(_contentWidth<=width&&_contentHeight>=height)
			{
				scale=height/_contentHeight;
			}
			else if(_contentWidth>=width&&_contentHeight<=height)
			{
				scale=width/_contentWidth;
			}
			else if(_contentWidth<=width&&_contentHeight<=height){
				scale=1;
			}
			_loader.scaleX=_loader.scaleY=scale;
			_loader.x=(width-_loader.width)*.5;
			_loader.y=(height-_loader.height)*.5;
			addChild(_loader);
			
			_cutMask.reset();
			if(_cutMask.width>_loader.width&&_cutMask.height>_loader.height)
			{
				scale=_cutMask.width/_loader.width>_cutMask.height/_loader.height?_loader.width/_cutMask.width:_loader.height/_cutMask.height;
			}
			else if(_cutMask.width<=_loader.width&&_cutMask.height>=_loader.height)
			{
				scale=_loader.height/_cutMask.height;
			}
			else if(_cutMask.width>=_loader.width&&_cutMask.height<=_loader.height)
			{
				scale=_loader.width/_cutMask.width;
			}
			else if(_cutMask.width<=_loader.width&&_cutMask.height<=_loader.height){
				scale=1;
			}
			_cutMask.scaleX=_cutMask.scaleY=1;
			_cutMask.x=_loader.x;
			_cutMask.y=_loader.y;
			addChild(_cutMask);
			
			if(_tm)
			{
				_tm.destroy();
			}
			_tm=new TransformManager({
				targetObjects:[_cutMask],
				allowDelete:false,
				allowMultiSelect:false,
				arrowKeysMove:true,
				autoDeselect:false,
				constrainScale:true,
				lockRotation:true,
				forceSelectionToFront:false,
				handleFillColor:0xffffff,
				handleSize:5,
				lineColor:0x3399fe,
				lineThickness:1,
				bounds:new Rectangle(_loader.x,_loader.y,_loader.width,_loader.height)
			});
			_tm.selectedItems=[_cutMask];
			var item:TransformItem=_tm.getItem(_cutMask);
			item.minScale=.2;
			_tm.addEventListener(TransformEvent.FINISH_INTERACTIVE_MOVE,onFinished);
			_tm.addEventListener(TransformEvent.FINISH_INTERACTIVE_SCALE,onFinished);
			onFinished();
		}
	
		private var _selectedRectangle:Rectangle;
		private function onFinished(e:TransformEvent=null):void
		{
			_selectedRectangle=_cutMask.getBounds(_loader);
			dispatchEvent(new Event(Event.CHANGE));
		}
		public override function get width():Number
		{
			return _width;
		}

		public override function get height():Number
		{
			return _height;
		}

		public function get loader():Loader
		{
			return _loader;
		}

		public function get selectedRectangle():Rectangle
		{
			return _selectedRectangle;
		}


	}
}